package com.example.littleSpider.myapplication.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.littleSpider.myapplication.R;
import com.example.littleSpider.myapplication.adapter.MySaleAdapter;
import com.example.littleSpider.myapplication.adapter.MySaleItemDecoration;
import com.example.littleSpider.myapplication.base.BaseActivity;
import com.example.littleSpider.myapplication.myInterfaces.MethodInTitle;
import com.example.littleSpider.myapplication.view.TitleView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.littlespider.mylibrary.util.T;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;

/**
 * 从特卖界面点击进入的我的特卖界面
 * Created by aduser on 2017/12/9.
 */

public class MyEspeciallySaleActivity extends BaseActivity implements MethodInTitle{

    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refresh_view)
    TwinklingRefreshLayout refreshView;
    @BindViews({R.id.tv_tag_one, R.id.tv_tag_two, R.id.tv_tag_three, R.id.tv_tag_four, R.id.tv_tag_five})
    List<TextView> tv_tags;
    @BindViews({R.id.tag_one, R.id.tag_two, R.id.tag_three, R.id.tag_four, R.id.tag_five})
    List<View> tagViews;

    List<String> datas = new ArrayList<>();
    private int clickNum = -1;
    private MySaleAdapter mySaleAdapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_my_especially_sale;
    }

    @Override
    protected void initView() {
        refreshView.setOnRefreshListener(new RefreshAndLoad());
        titleView.initMyTitle(getString(R.string.my_especially_sale), this, false);
        titleView.hideTitleTextImage();
        for (int i = 0; i < 8; i++) {
//            datas.add("");
        }
        mySaleAdapter = new MySaleAdapter(R.layout.item_sales_return_and_refund, datas);
        View view = LayoutInflater.from(this).inflate(R.layout.view_banner, null);
        mySaleAdapter.setEmptyView(view);
        recyclerView.setAdapter(mySaleAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(MyEspeciallySaleActivity.this));
        recyclerView.addItemDecoration(new MySaleItemDecoration());
        recyclerView.setFocusable(false);
        mySaleAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(MyEspeciallySaleActivity.this, RefundActivity.class);
                startActivity(intent);
            }
        });
        //如果缺少这句话, recyclerView将会滑动粘连
        recyclerView.setNestedScrollingEnabled(false);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void setGoBack() {
        finish();
    }

    @Override
    public void onRightOne() {
        Intent intent = new Intent(MyEspeciallySaleActivity.this, ShoppingCartActivity.class);
        startActivity(intent);
    }

    @Override
    public void onRightTwo() {

    }

    @Override
    public void onTitleText() {

    }

    @OnClick(R.id.ll_sales_return_or_refund)
    public void salesReturn(){
        Intent intent = new Intent(MyEspeciallySaleActivity.this, SalesReturnActivity.class);
        startActivity(intent);
    }

    /**
     * 切换列表数据
     * @param view
     */
    @OnClick({R.id.rl_group_one, R.id.rl_group_two, R.id.rl_group_three, R.id.rl_group_four, R.id.rl_group_five})
    public void groupClicks(View view){
        datas.clear();
        //初始化列表标签颜色
        for (int i = 0; i < tv_tags.size(); i++) {
            tv_tags.get(i).setTextColor(Color.parseColor("#8A000000"));
            tagViews.get(i).setVisibility(View.GONE);
        }
        switch (view.getId()){
            case R.id.rl_group_one: //全部
                clickNum = 0;
                for (int i = 0; i < 0; i++) {
                    datas.add("");
                }
                break;
            case R.id.rl_group_two: //待付款
                clickNum = 1;
                for (int i = 0; i < 5; i++) {
                    datas.add("");
                }
                break;
            case R.id.rl_group_three: //待发货
                clickNum = 2;
                for (int i = 0; i < 3; i++) {
                    datas.add("");
                }
                break;
            case R.id.rl_group_four: //待收货
                clickNum = 3;
                for (int i = 0; i < 5; i++) {
                    datas.add("");
                }
                break;
            case R.id.rl_group_five: //待评价
                clickNum = 4;
                for (int i = 0; i < 0; i++) {
                    datas.add("");
                }
                break;
            default:
                break;
        }
        changeColorOfViews();
        mySaleAdapter.notifyDataSetChanged();
    }

    /**
     * 改变当前标签的文字和View颜色
     */
    private void changeColorOfViews(){
        tv_tags.get(clickNum).setTextColor(Color.parseColor("#0092DF"));
        tagViews.get(clickNum).setVisibility(View.VISIBLE);
    }

    class RefreshAndLoad extends RefreshListenerAdapter{

        @Override
        public void onRefresh(TwinklingRefreshLayout refreshLayout) {
            super.onRefresh(refreshLayout);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                   T.show(MyEspeciallySaleActivity.this, "刷新");
                    refreshView.finishRefreshing();
                }
            }, 1000);
        }

        @Override
        public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
            super.onLoadMore(refreshLayout);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    T.show(MyEspeciallySaleActivity.this, "加载");
                    refreshView.finishLoadmore();
                }
            }, 1000);
        }
    }
}
